//  https://www.nowcoder.com/practice/38ae72379d42471db1c537914b06d48e?tpId=230&tqId=39755&ru=/exam/oj

#include <iostream>
#include<cstring>
using namespace std;


int n, m;
const int N = 510;
int dp[N][N];

int main()
{
    cin >> n >> m;
    memset(dp, 0x3f3f3f3f, sizeof dp);
    dp[0][1] = 0;
    for (int i = 1;i <= n;i++)
    {
        for (int j = 1;j <= m;j++)
        {
            int x;cin >> x;
            dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + x;
        }
    }

    cout << dp[n][m] << endl;

}